package com.note.controller;

import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.note.pojo.*;
import com.note.service.AdminService;
import com.note.service.CommService;
import com.note.service.NoteService;
import com.note.service.UserService;
import com.note.vo.ComNnUn;
import com.note.vo.NoteUname;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/admin")
public class AdminsController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private UserService userService;
    @Autowired
    private NoteService noteService;
    @Autowired
    private CommService commService;

    @RequestMapping("/tolog")
    public String tolog(){
        return "admins/login";
    }

    @ResponseBody
    @RequestMapping("/log")
    public String log(String name, String pwds, HttpSession session){
        String pwd= SecureUtil.md5(pwds);
        Admins admin = adminService.getAdminByName(name);
        if (admin!=null){
            if (pwd.equals(admin.getPwd())){
                session.setAttribute("admin",admin);
                return "100";     //登陆成功
            }else {
                return "101";     //密码不正确
            }
        }else {
            return "102";    //不存在该管理员
        }
    }

    @RequestMapping("/main")
    public String main(){
        return "admins/admain";
    }

    //安全退出
    @RequestMapping("/out")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/admin/tolog";
    }
    //管理员后台首页iframe
    @RequestMapping("/info")
    public String info(HttpSession session, Model model){
        Admins admin = (Admins) session.getAttribute("admin");
        if (admin.getType()==0){
            String quanxian="超级管理员";
            model.addAttribute("quanxian",quanxian);
        }else{
            String quanxian="普通管理员";
            model.addAttribute("quanxian",quanxian);
        }
        model.addAttribute("admin",admin);
        return "admins/admininfo";
    }
    //展示所有用户
    @RequestMapping("/listUser")
    public String listUser(@RequestParam(value = "pn",defaultValue = "1") Integer pn,Model model){
        PageHelper.startPage(pn,6);
        List<User> users = adminService.getUsers();
        PageInfo page = new PageInfo(users, 5);
        model.addAttribute("pageInfo",page);
        return "admins/listUser";
    }
    //删除用户
    @RequestMapping("/delUser")
    public String delUser(Integer id,Integer pn){
        adminService.delUser(id);
        return "redirect:/admin/listUser?pn="+pn;
    }

    //显示所有笔记
    @RequestMapping("/listNote")
    public String listNote(@RequestParam(value = "pn",defaultValue = "1") Integer pn,Model model){
        int size =(int) Math.ceil((double) adminService.getNotes().size()/8);
        int[] sizes=new int[size];
        for (int i = 0; i < sizes.length ; i++) {
            sizes[i]=i+1;
        }
        PageHelper.startPage(pn,8);
        List<Note> lists = adminService.getNotes();
        List<NoteUname> notes = lists.stream().map(list -> {
            NoteUname note=new NoteUname();
            BeanUtils.copyProperties(list,note);
            note.setId(list.getId());
            note.setContent(list.getContent());
            note.setCreatetime(list.getCreatetime());
            note.setTitle(list.getTitle());
            note.setType(list.getType());
            note.setUserid(list.getUserid());
            note.setName(userService.findNameById(list.getUserid()));
            return note;
        }).collect(Collectors.toList());
        System.out.println(notes.size());
        PageInfo pageInfo=new PageInfo(notes,size);
        pageInfo.setPageNum(pn);
        pageInfo.setNavigatepageNums(sizes);
        pageInfo.setPages(size);
        model.addAttribute("pageInfo",pageInfo);
        return "admins/listNote";
    }

    //删除笔记
    @RequestMapping("/delNote")
    public String delNote(Integer id,Integer pn){
        noteService.deleteNoteById(id);
        return "redirect:/admin/listNote?pn="+pn;
    }

    //展示所有评论
    @RequestMapping("/listCom")
    public String listCom(@RequestParam(value = "pn",defaultValue = "1") Integer pn,Model model){
        int size =(int) Math.ceil((double) adminService.getComs().size()/8);
        int[] sizes=new int[size];
        for (int i = 0; i < sizes.length ; i++) {
            sizes[i]=i+1;
        }
        PageHelper.startPage(pn,8);
        List<Comm> comms = adminService.getComs();
        List<ComNnUn> coms=comms.stream().map(comm -> {
            ComNnUn com=new ComNnUn();
            BeanUtils.copyProperties(comm,com);
            com.setId(comm.getId());
            com.setContents(comm.getContents());
            com.setCreatetime(comm.getCreatetime());
            com.setLikes(comm.getLikes());
            com.setNoteid(comm.getNoteid());
            com.setUserid(comm.getUserid());
            if (comm.getUserid()!=null){
                com.setComname(userService.findNameById(comm.getUserid()));
            }else{
                com.setComname("匿名");
            }
            if (noteService.getNoteById(comm.getNoteid())!=null){
                com.setNotetitle((noteService.getNoteById(comm.getNoteid())).getTitle());
                com.setNotuname(userService.findNameById(noteService.getNoteById(comm.getNoteid()).getUserid()));
            }else {
                com.setNotetitle("该笔记已被删除");
                com.setNotuname("笔记删除用户不予显示");
            }
            return com;
        }).collect(Collectors.toList());
        PageInfo pageInfo=new PageInfo(coms,size);
        pageInfo.setPageNum(pn);
        pageInfo.setNavigatepageNums(sizes);
        pageInfo.setPages(size);
        model.addAttribute("pageInfo",pageInfo);
        return "admins/listCom";
    }

    //删除评论
    @RequestMapping("/delCom")
    public String delCom(Integer id,Integer pn){
        commService.delComById(id);
        return "redirect:/admin/listCom?pn="+pn;
    }

    //跳转到增加管理员页面
    @RequestMapping("/addadmin")
    public String addadmin(){
        return "admins/addadmin";
    }
    //保存管理员提交
    @ResponseBody
    @RequestMapping("/addpost")
    public String addpost(String name,String pwd,String email){
        Admins admins = new Admins();
        admins.setName(name);
        admins.setPwd(SecureUtil.md5(pwd));
        admins.setEmail(email);
        admins.setTime(new Date());
        adminService.addAdmin(admins);
        return "suc";
    }
    //跳转到显示所有管理员页面
    @RequestMapping("/listAdm")
    public String listAdm(Model model){
        List<Admins> adms = adminService.getAdms();
        model.addAttribute("admins",adms);
        return "admins/listAdm";
    }

    //删除管理员
    @RequestMapping("/delAdm")
    public String delAdm(Integer id){
        adminService.delAdm(id);
        return "redirect:/admin/listAdm";
    }

}
